import * as qiniu from 'qiniu-js'

const install = (Vue, vm) => {

	Vue.prototype.$fileUpload = (file) => {
		return new Promise((resolve, reject) => {
			vm.$u.api.getUptoken().then(res => {
				const {
					ResultCode,
					ResultMessage
				} = res;
				if (ResultCode === 1) {
					const token = JSON.parse(ResultMessage).uptoken;
					const key =  vm.$u.date(null, 'yyyymmddhhMMss')+ '_' + file.name
					const observable = qiniu.upload(file, key , token);
					observable.subscribe({
						complete(res) {
							resolve(res);
						},
						error(err) {
							reject(err)
						}
					})
				}
			})
		})
	}
}

export default {
	install
};
